/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;



/**
 *
 * @author Kenny
 */
public class NewStaffPanel extends javax.swing.JPanel {

    Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    private JFileChooser chooser,jfcImageChooser;
    private FileNameExtensionFilter filter;
    private File image;
    private ImageIcon photo;
    private Image scaleImage;
    private Map<String, Object> imageData;
    private Map<String, Object> data = new HashMap<>();
    /**
     * Creates new form RoutePanel
     */
    int yearToday;
    public NewStaffPanel() {
        initComponents();
        Calendar now = Calendar.getInstance();
        yearToday = now.get(Calendar.YEAR);
        jbtCheckIC.setEnabled(false);
        jbtCheckUsername.setEnabled(false);
        jtfUsername.setEditable(false);
        jrbMale.setSelected(true);
        disableAll();
        setDefault();
        
    }
    
        public void disableAll(){
        jbtCheckUsername.setEnabled(false);
        jtfUsername.setEditable(false);
        jtfFirstName.setEditable(false);
        jtfLastName.setEditable(false);
        jtfDoB.setEditable(false);
        jrbMale.setEnabled(false);
        jrbFemale.setEnabled(false);
        jcbPosition.setEnabled(false);
        jtfPhoneNo.setEditable(false);
        jtfEmail.setEditable(false);
        jtfAddress.setEditable(false);
        jtfCity.setEditable(false);
        jtfState.setEditable(false);
        jtfPostalCode.setEditable(false);
        jcbQuestion.setEnabled(false);
        jtfAnswer.setEditable(false);
        jbAdd.setEnabled(false);
        jtfYearEmployed.setEnabled(false);
        
    }
    
    public void enableAll(){
        jtfIC.setEditable(true);
        jtfFirstName.setEditable(true);
        jtfLastName.setEditable(true);
        jtfDoB.setEditable(true);
        jrbMale.setEnabled(true);
        jrbFemale.setEnabled(true);
        jcbPosition.setEnabled(true);
        jtfPhoneNo.setEditable(true);
        jtfEmail.setEditable(true);
        jtfAddress.setEditable(true);
        jtfCity.setEditable(true);
        jtfState.setEditable(true);
        jtfPostalCode.setEditable(true);
        jcbQuestion.setEnabled(true);
        jtfAnswer.setEditable(true);
        jbAdd.setEnabled(true);
    }
    
    public void setDefault(){
        jtfIC.setEditable(true);
        jtfUsername.setEditable(false);
        jtfUsername.setText(null);
        jtfFirstName.setText(null);
        jtfLastName.setText(null);
        jtfDoB.setText(null);
        jcbPosition.setSelectedIndex(0);
        jtfPhoneNo.setText(null);
        jtfEmail.setText(null);
        jtfAddress.setText(null);
        jtfCity.setText(null);
        jtfState.setText(null);
        jtfPostalCode.setText(null);
        jcbQuestion.setSelectedIndex(0);
        jtfAnswer.setText(null);
        jbAdd.setEnabled(false);
        jtfYearEmployed.setText(""+yearToday);
    }
    
    private void generateByteDataFromImageFile(File file) {
        BufferedImage image;
        try {
            image = ImageIO.read(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, file.getName().substring(file.getName().lastIndexOf(".") + 1), baos);
            byte[] imageByte = baos.toByteArray();
            String encoded = Base64.encodeBase64String(imageByte);
            data.put("imagedata", encoded);
            data.put("imagename", file.getName());
        } catch (IOException | IllegalArgumentException ex) {
            Toast.makeText(this, "This is not a valid image file.", Toast.LENGTH_SHORT).display();
        }
    }
    
    public static boolean isNumeric(String str){  
        try{  
            double d = Double.parseDouble(str);  
        }  
        catch(NumberFormatException nfe){  
            return false;  
        }  
        return true;  
    }
    
    public String generateRandomPassword(){
        SecureRandom random = new SecureRandom();
        return new BigInteger(36, random).toString(10);
    }
    
    private Map<String, Object> getAllInputData(){
        data.put("username", jtfUsername.getText());
        data.put("icNo",jtfIC.getText());
        data.put("password",generateRandomPassword());
        data.put("firstname",jtfFirstName.getText());
        data.put("lastname", jtfLastName.getText());
        data.put("dob",jtfDoB.getText());
        if(jrbMale.isSelected()){
            data.put("gender","M");
        }else{
            data.put("gender","F");
        }
        data.put("position",jcbPosition.getSelectedItem().toString());
        data.put("yearEmployed", jtfYearEmployed.getText());
        data.put("address", jtfAddress.getText());
        data.put("city", jtfCity.getText());
        data.put("state", jtfState.getText());
        data.put("postalCode", jtfPostalCode.getText());
        data.put("contactNo", jtfPhoneNo.getText());
        data.put("email", jtfEmail.getText());
        data.put("question", jcbQuestion.getSelectedItem().toString());
        data.put("answer", jtfAnswer.getText());
        data.put("status","Available");
        return data;
    }

    
    public static boolean isValidEmailAddress(String email) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(email);
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    return result;
}
    
    public static boolean containsWhiteSpace(final String testCode){
        if(testCode != null){
            for(int i = 0; i < testCode.length(); i++){
                if(Character.isWhitespace(testCode.charAt(i))){
                    return true;
                }
            }
        }
        return false;
    }
    
private void copyTo(File get, File dest){
    try{
        File newDest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());

        java.io.FileInputStream fis = new java.io.FileInputStream(get);
        if (dest.exists())
        dest.delete();
        dest.mkdirs();
        dest.setWritable(true);
        dest.setReadable(true);

        System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
        java.io.FileOutputStream fos = new java.io.FileOutputStream(newDest);
        int b;
        do{
            b = fis.read();
            fos.write(b);
        }while (b != -1);
        }
        catch (FileNotFoundException ex){
            System.err.println("404: \"" + get + "\"");
            ex.printStackTrace();
        }
        catch (java.io.IOException ex){
            System.err.println("IO exception on \"" + get + "\"");
            ex.printStackTrace();
        }
  }


public void copyFiles(File sourceLocation , File targetLocation)throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }
            File[] files = sourceLocation.listFiles();
            for(File file:files){
                InputStream in = new FileInputStream(file);
                OutputStream out = new FileOutputStream(targetLocation+"/"+file.getName());

                // Copy the bits from input stream to output stream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }            
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgGender = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jpStaffArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };
        jpStaff = new javax.swing.JPanel();
        jbAdd = new javax.swing.JButton();
        jbReset = new javax.swing.JButton();
        jpStaffParticular = new javax.swing.JPanel();
        jlblFirstName = new javax.swing.JLabel();
        jlblLastName = new javax.swing.JLabel();
        jtfLastName = new javax.swing.JTextField();
        jlblGender = new javax.swing.JLabel();
        jrbMale = new javax.swing.JRadioButton();
        jrbFemale = new javax.swing.JRadioButton();
        jlblPosition = new javax.swing.JLabel();
        jcbPosition = new javax.swing.JComboBox();
        jlblDoB = new javax.swing.JLabel();
        jtfDoB = new javax.swing.JTextField();
        jtfIC = new javax.swing.JTextField();
        jtfFirstName = new javax.swing.JTextField();
        jbtCheckIC = new javax.swing.JButton();
        jlblYearEmployed = new javax.swing.JLabel();
        jtfYearEmployed = new javax.swing.JTextField();
        jtfUsername = new javax.swing.JTextField();
        jlblUsername = new javax.swing.JLabel();
        jlblUsername1 = new javax.swing.JLabel();
        jbtCheckUsername = new javax.swing.JButton();
        jpAddress = new javax.swing.JPanel();
        jlblAddress = new javax.swing.JLabel();
        jlblCity = new javax.swing.JLabel();
        jlblState = new javax.swing.JLabel();
        jlblPostalCode = new javax.swing.JLabel();
        jtfCity = new javax.swing.JTextField();
        jtfState = new javax.swing.JTextField();
        jtfPostalCode = new javax.swing.JTextField();
        jtfAddress = new javax.swing.JTextField();
        jlblImage = new javax.swing.JLabel();
        jbtBrowse = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jpContacts = new javax.swing.JPanel();
        jlblPhoneNo = new javax.swing.JLabel();
        jlblEmail = new javax.swing.JLabel();
        jtfPhoneNo = new javax.swing.JTextField();
        jtfEmail = new javax.swing.JTextField();
        jpSecurity = new javax.swing.JPanel();
        jlblQuestion = new javax.swing.JLabel();
        jlblAnswer = new javax.swing.JLabel();
        jcbQuestion = new javax.swing.JComboBox();
        jtfAnswer = new javax.swing.JTextField();

        bgGender.add(jrbMale);
        bgGender.add(jrbFemale);

        jLabel2.setText("jLabel2");

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        javax.swing.GroupLayout jpStaffArrowLayout = new javax.swing.GroupLayout(jpStaffArrow);
        jpStaffArrow.setLayout(jpStaffArrowLayout);
        jpStaffArrowLayout.setHorizontalGroup(
            jpStaffArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpStaffArrowLayout.setVerticalGroup(
            jpStaffArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpStaffArrow);
        jpStaffArrow.setBounds(0, 0, 150, 20);

        jpStaff.setBackground(new java.awt.Color(220, 220, 220));

        jbAdd.setText("Add");
        jbAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddActionPerformed(evt);
            }
        });

        jbReset.setText("Reset");
        jbReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbResetActionPerformed(evt);
            }
        });

        jpStaffParticular.setBorder(javax.swing.BorderFactory.createTitledBorder("Staff Particular"));

        jlblFirstName.setText("IC");

        jlblLastName.setText("Last Name");

        jtfLastName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfLastNameKeyReleased(evt);
            }
        });

        jlblGender.setText("Gender");

        jrbMale.setText("Male");

        jrbFemale.setText("Female");

        jlblPosition.setText("Position");

        jcbPosition.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Staff" }));

        jlblDoB.setText("DoB");

        jtfIC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfICKeyReleased(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfICKeyPressed(evt);
            }
        });

        jtfFirstName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfFirstNameKeyReleased(evt);
            }
        });

        jbtCheckIC.setText("Check");
        jbtCheckIC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCheckICActionPerformed(evt);
            }
        });

        jlblYearEmployed.setText("Year Employed");

        jtfUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfUsernameActionPerformed(evt);
            }
        });

        jlblUsername.setText("Username");

        jlblUsername1.setText("First Name");

        jbtCheckUsername.setText("Check");
        jbtCheckUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCheckUsernameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpStaffParticularLayout = new javax.swing.GroupLayout(jpStaffParticular);
        jpStaffParticular.setLayout(jpStaffParticularLayout);
        jpStaffParticularLayout.setHorizontalGroup(
            jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                        .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                        .addComponent(jlblFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(14, 14, 14))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpStaffParticularLayout.createSequentialGroup()
                                        .addComponent(jlblUsername1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)))
                                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                        .addComponent(jtfIC, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jbtCheckIC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                        .addComponent(jtfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                        .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jbtCheckUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                .addComponent(jlblLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jtfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpStaffParticularLayout.createSequentialGroup()
                        .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpStaffParticularLayout.createSequentialGroup()
                                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jlblUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jlblDoB, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(jtfDoB))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpStaffParticularLayout.createSequentialGroup()
                                .addComponent(jlblGender, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jrbMale, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jrbFemale, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))
                        .addGap(141, 141, 141))
                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                        .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                .addComponent(jlblYearEmployed)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jtfYearEmployed, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                                .addComponent(jlblPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jcbPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jpStaffParticularLayout.setVerticalGroup(
            jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfIC, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblFirstName)
                    .addComponent(jbtCheckIC))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbtCheckUsername))
                    .addComponent(jlblUsername))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblUsername1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblLastName)
                    .addComponent(jtfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblDoB)
                    .addComponent(jtfDoB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblGender)
                    .addComponent(jrbMale)
                    .addComponent(jrbFemale))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPosition)
                    .addComponent(jcbPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfYearEmployed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblYearEmployed))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpAddress.setBorder(javax.swing.BorderFactory.createTitledBorder("Address"));

        jlblAddress.setText("Address");

        jlblCity.setText("City");

        jlblState.setText("State");

        jlblPostalCode.setText("Postal Code");

        jbtBrowse.setText("Browse");
        jbtBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtBrowseActionPerformed(evt);
            }
        });

        jLabel3.setText("Picture");

        javax.swing.GroupLayout jpAddressLayout = new javax.swing.GroupLayout(jpAddress);
        jpAddress.setLayout(jpAddressLayout);
        jpAddressLayout.setHorizontalGroup(
            jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpAddressLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jlblAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlblCity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlblState, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlblPostalCode, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpAddressLayout.createSequentialGroup()
                        .addComponent(jlblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtBrowse))
                    .addComponent(jtfState)
                    .addComponent(jtfCity)
                    .addComponent(jtfPostalCode)
                    .addComponent(jtfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jpAddressLayout.setVerticalGroup(
            jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpAddressLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAddress)
                    .addComponent(jtfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblCity)
                    .addComponent(jtfCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblState)
                    .addComponent(jtfState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPostalCode)
                    .addComponent(jtfPostalCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpAddressLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jpAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jbtBrowse)
                            .addComponent(jlblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jpAddressLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        jpContacts.setBorder(javax.swing.BorderFactory.createTitledBorder("Contacts"));

        jlblPhoneNo.setText("Phone No");

        jlblEmail.setText("Email");

        javax.swing.GroupLayout jpContactsLayout = new javax.swing.GroupLayout(jpContacts);
        jpContacts.setLayout(jpContactsLayout);
        jpContactsLayout.setHorizontalGroup(
            jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpContactsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jlblPhoneNo, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(jlblEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfPhoneNo, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(131, Short.MAX_VALUE))
        );
        jpContactsLayout.setVerticalGroup(
            jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpContactsLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPhoneNo)
                    .addComponent(jtfPhoneNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblEmail)
                    .addComponent(jtfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jpSecurity.setBorder(javax.swing.BorderFactory.createTitledBorder("Security"));

        jlblQuestion.setText("Question");

        jlblAnswer.setText("Answer");

        jcbQuestion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "What was your childhood nickname?", "What street did you live on in third grade?", "In what city or town did your mother and father meet?", "What was the last name of your third grade teacher?", "What time of the day were you born?", "In what city or town was your first job?", "What was your favorite place to visit as a child?" }));

        javax.swing.GroupLayout jpSecurityLayout = new javax.swing.GroupLayout(jpSecurity);
        jpSecurity.setLayout(jpSecurityLayout);
        jpSecurityLayout.setHorizontalGroup(
            jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpSecurityLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jlblQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                    .addComponent(jlblAnswer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jcbQuestion, 0, 251, Short.MAX_VALUE)
                    .addComponent(jtfAnswer))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jpSecurityLayout.setVerticalGroup(
            jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpSecurityLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblQuestion)
                    .addComponent(jcbQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpSecurityLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAnswer)
                    .addComponent(jtfAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout jpStaffLayout = new javax.swing.GroupLayout(jpStaff);
        jpStaff.setLayout(jpStaffLayout);
        jpStaffLayout.setHorizontalGroup(
            jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffLayout.createSequentialGroup()
                .addGroup(jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpStaffLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jpStaffParticular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(jpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpStaffLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jpContacts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(jpSecurity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpStaffLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbReset, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(jbAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );
        jpStaffLayout.setVerticalGroup(
            jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpStaffParticular, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(11, 11, 11)
                .addGroup(jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpContacts, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpSecurity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpStaffLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbReset)
                    .addComponent(jbAdd))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        add(jpStaff);
        jpStaff.setBounds(0, 20, 870, 440);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtCheckICActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCheckICActionPerformed
    String staffInfo = RestServer.httpGet("searchStaffExistByIc", ""+jtfIC.getText());
        try{
            JSONObject json = new JSONObject(staffInfo);
            JSONObject content = json.getJSONObject("content");
            if(content.getString("exist").equals("NO")){
                Toast.makeText(getParent(), "IC is available", Toast.LENGTH_LONG).display();
                jtfIC.setEnabled(false);
                jbtCheckIC.setEnabled(false);
                jbtCheckUsername.setEnabled(true);
                jtfUsername.setEditable(true);
                //enableAll();
               
            }else{
                Toast.makeText(getParent(), "This IC is already exist in the database,\n please try another IC", Toast.LENGTH_LONG).display();
            }
            
        }catch(JSONException ex){
             Toast.makeText(getParent(), ""+ex, Toast.LENGTH_LONG).display();
        }
    }//GEN-LAST:event_jbtCheckICActionPerformed

    private void jbResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbResetActionPerformed
        jtfIC.setEnabled(true);
        jbtCheckIC.setEnabled(false);
        jtfIC.setText(null);
        jlblImage.setIcon(null);
        disableAll();
        setDefault();
    }//GEN-LAST:event_jbResetActionPerformed

    private void jtfICKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfICKeyPressed

    }//GEN-LAST:event_jtfICKeyPressed

    private void jtfICKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfICKeyReleased
        if(jtfIC.getText().length()==12 && isNumeric(jtfIC.getText())){
            jbtCheckIC.setEnabled(true);
        }else{
            jbtCheckIC.setEnabled(false);
        }
    }//GEN-LAST:event_jtfICKeyReleased

    private void jtfFirstNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfFirstNameKeyReleased
//        if(isNumeric(jtfFirstName.getText())){
//            jlblErrorLastName.setFont(new Font("Serif", Font.PLAIN, 9));
//            jlblErrorLastName.setForeground(Color.red);
//            jlblErrorFirstName.setText("Error!");
//            jlblErrorFirstName.show();
//        }else{
//            jlblErrorLastName.setFont(new Font("Serif", Font.PLAIN, 9));
//            jlblErrorLastName.setForeground(Color.white);
//            jlblErrorFirstName.setText(null);
//            jlblErrorFirstName.hide();
//        }
    }//GEN-LAST:event_jtfFirstNameKeyReleased

    private void jtfLastNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfLastNameKeyReleased
//
//        if(isNumeric(jtfLastName.getText())){
//            jlblErrorLastName.setFont(new Font("Serif", Font.PLAIN, 9));
//            jlblErrorLastName.setForeground(Color.red);
//            jlblErrorLastName.setText("Error!");
//            jlblErrorLastName.show();
//        }else{
//            jlblErrorLastName.setFont(new Font("Serif", Font.PLAIN, 9));
//            jlblErrorLastName.setForeground(Color.white);
//            jlblErrorLastName.setText(null);
//            jlblErrorLastName.hide();
//        }
    }//GEN-LAST:event_jtfLastNameKeyReleased

    private void jbAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAddActionPerformed
        Pattern patternFirstName = Pattern.compile("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$");
        Matcher matcherFirstName = patternFirstName.matcher(jtfFirstName.getText());
        boolean matchFirstName = matcherFirstName .matches();
        
        Pattern patternLastName = Pattern.compile("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$");
        Matcher matcherLastName = patternLastName.matcher(jtfLastName.getText());
        boolean matchLastName = matcherLastName .matches();
        
        Pattern patternEmail = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher matcherEmail = patternEmail.matcher(jtfEmail.getText());
        boolean matchEmail = matcherEmail.matches();
        
        Pattern patternPhone = Pattern.compile("\\d{3}-\\d{7}");
        Matcher matcherPhone = patternPhone.matcher(jtfPhoneNo.getText());
        boolean matchPhone = matcherPhone.matches();

        Pattern patternCity = Pattern.compile("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$");
        Matcher matcherCity = patternCity.matcher(jtfCity.getText());
        boolean matchCity = matcherCity.matches();

        Pattern patternState = Pattern.compile("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$");
        Matcher matcherState = patternState.matcher(jtfState.getText());
        boolean matchState = matcherState.matches();
        
        StringBuilder errors = new StringBuilder("Errors :\n");
        boolean validate = true;
        
        if(jtfFirstName.getText().length()==0){
            errors.append("First Name cannot be empty\n");
            validate = false;
        }else if(matchFirstName){
        }else if(!matchFirstName){
            errors.append("First Name cannot contain numeric characters\n");
            validate = false;
        }       
        
            
        if(jtfLastName.getText().length()==0){
            errors.append("Last Name cannot be empty\n");
            validate = false;
        }else if(matchLastName){
        }else if(!matchLastName){
            errors.append("Last Name cannot contain numeric characters\n");
            validate = false;
        }
        
        if(jtfPhoneNo.getText().length()==0){
            errors.append("Phone number cannot be empty\n");
            validate = false;
        }else if(matchPhone){
        }else if(!matchPhone){
            errors.append("Phone format is invalid  E.g. XXX-XXXXXXX\n");
            validate = false;
        }
        
        if(jtfEmail.getText().length()==0){
            errors.append("Email cannot be empty\n");
            validate = false;
        }else if(isValidEmailAddress(jtfEmail.getText())){
        }else{
            errors.append("Email format is invalid  E.g. name@email.com\n");
            validate = false;
        }
        
        if(jtfAddress.getText().length()==0){
            errors.append("Address cannot be empty\n");
            validate = false;
        }
        
        if(jtfCity.getText().length()==0){
            errors.append("City cannot be empty\n");
            validate = false;
        }else if(matchCity){
        }else{
            errors.append("City can only contains alphabets\n");
            validate = false;
        }
        
        if(jtfState.getText().length()==0){
            errors.append("State cannot be empty\n");
            validate = false;
        }else if(matchState){
        }else{
            errors.append("State can only contains alphabets\n");
            validate = false;
        }
        
        if(jtfPostalCode.getText().length()==0){
            errors.append("Postal Code cannot be empty\n");
        }else if(isNumeric(jtfPostalCode.getText()) &&  jtfPostalCode.getText().length()==5){
        }else{
            errors.append("Postal Code can only be numeric and contains 5 digits E.g. 53300\n");
            validate = false;
        }
        
        if(jtfAnswer.getText().length()==0){
            errors.append("Answer cannot be empty\n");
            validate = false;
        }
        
        if(jlblImage.getIcon()==null){
            errors.append("Please select a photo!");
            validate =false;
        }
        
        if(validate){
            String b = RestServer.httpPost("insertStaff",getAllInputData());
            try{
                System.out.println(b);
                JSONObject json = new JSONObject(b);
                if(json.getInt("status")==200){
                    Toast.makeText(getParent(), "Success", Toast.LENGTH_LONG).display();
                }else{
                    Toast.makeText(getParent(), ""+json.getString("message"), Toast.LENGTH_LONG).display();
                }
            }catch (JSONException ex) {
                Toast.makeText(getParent(), ""+ex, Toast.LENGTH_LONG).display();
                System.out.println(ex);
            }
            
        }else{
            JOptionPane.showMessageDialog(null, errors);
//            Toast.makeText(getParent(), errors, Toast.LENGTH_LONG).display();
        }


    }//GEN-LAST:event_jbAddActionPerformed

    private void jbtBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBrowseActionPerformed
        chooser =   new JFileChooser();
        filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
        chooser.addChoosableFileFilter(filter);
        int i = chooser.showOpenDialog(jlblImage);
        if(i==JFileChooser.APPROVE_OPTION)
        {
            image = chooser.getSelectedFile();
            photo   =   new ImageIcon(image.getAbsolutePath());
            scaleImage = photo.getImage().getScaledInstance(91, 91, Image.SCALE_DEFAULT);
            jlblImage.setIcon(new ImageIcon(scaleImage));
            generateByteDataFromImageFile(chooser.getSelectedFile());
        }
    }//GEN-LAST:event_jbtBrowseActionPerformed

    private void jtfUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfUsernameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfUsernameActionPerformed

    private void jbtCheckUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCheckUsernameActionPerformed
       
        
        boolean isWhitespace = containsWhiteSpace(jtfUsername.getText());
        if(!isWhitespace){
            String staffInfo = RestServer.httpGet("searchStaffExistByUsername", ""+jtfUsername.getText());
            try{
                JSONObject json = new JSONObject(staffInfo);
                JSONObject content = json.getJSONObject("content");
                if(content.getString("exist").equals("NO")){
                    Toast.makeText(getParent(), "Username available", Toast.LENGTH_LONG).display();
                    jbtCheckUsername.setEnabled(false);
                    jtfUsername.setEditable(false);
                    enableAll();
                }else{
                    Toast.makeText(getParent(), "This username is already exist in the database,\n please try another username", Toast.LENGTH_LONG).display();
                }

            }catch(JSONException ex){
                 Toast.makeText(getParent(), ""+ex, Toast.LENGTH_LONG).display();
            }
        }else{
            Toast.makeText(getParent(), "Username cannot contain space or special characters", Toast.LENGTH_LONG).display();
        }
    }//GEN-LAST:event_jbtCheckUsernameActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgGender;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbAdd;
    private javax.swing.JButton jbReset;
    private javax.swing.JButton jbtBrowse;
    private javax.swing.JButton jbtCheckIC;
    private javax.swing.JButton jbtCheckUsername;
    private javax.swing.JComboBox jcbPosition;
    private javax.swing.JComboBox jcbQuestion;
    private javax.swing.JLabel jlblAddress;
    private javax.swing.JLabel jlblAnswer;
    private javax.swing.JLabel jlblCity;
    private javax.swing.JLabel jlblDoB;
    private javax.swing.JLabel jlblEmail;
    private javax.swing.JLabel jlblFirstName;
    private javax.swing.JLabel jlblGender;
    private javax.swing.JLabel jlblImage;
    private javax.swing.JLabel jlblLastName;
    private javax.swing.JLabel jlblPhoneNo;
    private javax.swing.JLabel jlblPosition;
    private javax.swing.JLabel jlblPostalCode;
    private javax.swing.JLabel jlblQuestion;
    private javax.swing.JLabel jlblState;
    private javax.swing.JLabel jlblUsername;
    private javax.swing.JLabel jlblUsername1;
    private javax.swing.JLabel jlblYearEmployed;
    private javax.swing.JPanel jpAddress;
    private javax.swing.JPanel jpContacts;
    private javax.swing.JPanel jpSecurity;
    private javax.swing.JPanel jpStaff;
    private javax.swing.JPanel jpStaffArrow;
    private javax.swing.JPanel jpStaffParticular;
    private javax.swing.JRadioButton jrbFemale;
    private javax.swing.JRadioButton jrbMale;
    private javax.swing.JTextField jtfAddress;
    private javax.swing.JTextField jtfAnswer;
    private javax.swing.JTextField jtfCity;
    private javax.swing.JTextField jtfDoB;
    private javax.swing.JTextField jtfEmail;
    private javax.swing.JTextField jtfFirstName;
    private javax.swing.JTextField jtfIC;
    private javax.swing.JTextField jtfLastName;
    private javax.swing.JTextField jtfPhoneNo;
    private javax.swing.JTextField jtfPostalCode;
    private javax.swing.JTextField jtfState;
    private javax.swing.JTextField jtfUsername;
    private javax.swing.JTextField jtfYearEmployed;
    // End of variables declaration//GEN-END:variables
}
